package com.zhuoyue.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

import com.zhuoyue.biz.core.api.dto.rep.HomeRepDTO;
import com.zhuoyue.biz.core.api.service.impl.HomeService;
import com.zhuoyue.common.aop.auth.CheckLogin;
import com.zhuoyue.common.aop.auth.User;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import com.zhuoyue.biz.core.api.domain.HomeDO;

import com.zhuoyue.common.dto.ControllerResult;


/**
 * @author wjming
 * @ClassName: AppHomeController
 * @Description:用户家庭信息添加，获取，删除，修改操作
 * @Date 2021/2/14 19:55
 */
@RestController
@Scope("prototype")
@RequestMapping("/api/home")
@Validated
public class AppHomeController {


    @Autowired
    private HomeService homeService;


    /**
     * 新增家庭
     * 名称、图片路径即可,名称如果不写默认是手机号码后四位,比如账号是13868923544 手机号码就是,3544的家,默认图片是：default_home
     *
     * @param request
     * @param name
     * @param location 家庭位置（省市区）
     * @param address  家庭详细地址（xxxx街道xx小区xxxxx）
     * @return
     */
    @PostMapping("/add")
    @CheckLogin
    public ControllerResult addHome(HttpServletRequest request, @Length(max = 15, message = "不能超过15个字符") String name, String location, String address) {

        User user = (User) request.getAttribute("user");
        String account = user.getAccount();
        if (StringUtils.isEmpty(name)) {
            name = account.substring(account.length() - 4) + "的家";
        }

        HomeDO home = new HomeDO();
        home.setName(name);
        home.setImgUrl("home_01");
        home.setLocation(location);
        home.setAddress(address);
        homeService.addHome(home, user.getId());
        HomeRepDTO homeRepDTO = new HomeRepDTO();
        BeanUtils.copyProperties(home, homeRepDTO);
        homeRepDTO.setAppUserHomeRelation(0);
        homeRepDTO.setCreateTime(home.getCreateTime());
        return ControllerResult.toReturnDataSuccess("家庭添加成功", homeRepDTO);
    }


    /**
     * 获取家庭列表
     *
     * @param request
     * @return
     */
    @GetMapping("/list")
    @CheckLogin
    public ControllerResult getHomeList(HttpServletRequest request) {
        User user = (User) request.getAttribute("user");
        return ControllerResult.toReturnDataSuccess("获取成功", homeService.getHome(user.getId()));
    }


    /**
     * 修改家庭信息
     *
     * @param homeId
     * @param name
     * @param imgUrl
     * @param location 家庭位置（省市区）
     * @param address  家庭详细地址（xxxx街道xx小区xxxxx）
     * @return
     */
    @PutMapping("/modify")
    @CheckLogin
    public ControllerResult updateHomeInfo(@RequestParam @NotBlank(message = "家庭ID不能为空") String homeId,
                                           @Length(max = 15, message = "请输入1-15个字符") String name,
                                           String imgUrl, String location, String address, Double longitude, Double latitude) {
        HomeDO home = new HomeDO();
        home.setId(homeId);
        home.setLocation(location);
        home.setAddress(address);
        home.setName(StringUtils.isNotEmpty(name) ? name.trim() : null);
        home.setImgUrl(StringUtils.isNotEmpty(imgUrl) ? imgUrl.trim() : null);
        home.setLongitude(longitude);
        home.setLatitude(latitude);
        boolean result = homeService.updateById(home);
        if (!result) {
            return ControllerResult.toParamFail("修改失败");
        }
        return ControllerResult.toReturnMessageSuccess("修改成功");
    }


    /**
     * 删除家庭
     *
     * @param request
     * @param homeId
     * @return
     */
    @DeleteMapping("/delete")
    @CheckLogin
    public ControllerResult deleteHomeById(HttpServletRequest request, @NotBlank(message = "家庭ID不能为空") String homeId) {
        User user = (User) request.getAttribute("user");
        // 删除家庭
        homeService.deleteHomeById(user.getId(), homeId, user.getOrgCode());
        return ControllerResult.toReturnMessageSuccess("删除成功");
    }

}
